#!/usr/bin/env bash

progress() {
  echo -e "\n\033[44m${1}\033[0m\n"
}

parse_kbuild() {
  # Load config into shell variables
  eval `grep -o 'CONFIG[_A-Z0-9]* 1' include/autoconf.h | sed 's/ 1/=1/g'`

  for KBUILD in `find . -type f -name Kbuild`; do
    DIR=${KBUILD#./}
    DIR=${DIR%/*}
    grep '^[^#].*\.o\b' $KBUILD | while read LINE; do
      FILE_LIST=${LINE#*+=}
      CONFIG=`echo ${LINE%+=*} | grep -o '$(.*)' | cut -d\( -f2 | cut -d\) -f1`
      if eval [ -z \"$CONFIG\" -o \"\$$CONFIG\" = \"1\" ]; then
        for FILE in `echo $FILE_LIST | grep -o '\S*\.o\b'`; do
          readlink -f $DIR/$FILE | sed -e "s:${CWD}/::" -e 's/.o$/.c \\/g'
        done
      fi
    done
  done
}

generate_files() {
  # Copy config and make config
  progress "Generating configuration files"
  cp ../busybox.config .config
  yes n | make oldconfig >/dev/null 2>&1

  # Generate headers
  gcc applets/applet_tables.c -o applets/applet_tables
  applets/applet_tables include/applet_tables.h include/NUM_APPLETS.h
  gcc applets/usage.c -o applets/usage -Iinclude
  applets/usage_compressed include/usage_compressed.h applets
  scripts/mkconfigs include/bbconfigopts.h include/bbconfigopts_bz2.h
  scripts/generate_BUFSIZ.sh include/common_bufsiz.h
  srctree=$CWD HOSTCC=gcc scripts/embedded_scripts include/embedded_scripts.h embed applets_sh

  progress "Generating Android_src.mk based on configs"

  # Process Kbuild files
  echo "LOCAL_SRC_FILES := \\" > Android_src.mk
  parse_kbuild | sort -u >> Android_src.mk

  # Build Android.mk
  echo 'LOCAL_PATH := $(call my-dir)' > Android.mk
  cat Makefile | head -n 3 >> Android.mk
  cat ../busybox.mk >> Android.mk

  if $COMMIT; then
    progress "Commit headers and Makefiles"
    git add -f include/*.h
    git add *.mk
    git commit -m "Add generated files for ndk-build" -m "Auto generated by ndk-box-kitchen"
  fi
}

apply_patches() {
  local MSG=../msg
  echo -e 'Apply patches, courtesy of osm0sis/android-busybox-ndk\n' > $MSG
  echo -e 'Auto generated by ndk-box-kitchen\n' >> $MSG
  echo 'Applied patches:' >> $MSG
  for p in ../busybox_patches/*; do
    echo `basename $p` >> $MSG
    patch -p1 < $p
  done
  git add .
  git commit -F $MSG
  rm $MSG
}

if [ ! -d busybox ]; then
  progress "Please clone busybox, checkout to desired tag, apply patches, then run this script"
  exit 1
fi

cd busybox
CWD=`pwd -P`

case "$1" in
  generate )
    [ "$2" = "--commit" ] && COMMIT=true || COMMIT=false
    generate_files
    ;;
  patch )
    apply_patches
    ;;
  * )
    echo "Usage:"
    echo "$0 patch"
    echo "   Apply patches for busybox"
    echo "$0 generate [--commit]"
    echo "   Generate Makefiles for compilation"
    ;;
esac

cd ..
